#!/bin/bash
# Linux部署脚本

# 设置变量
APP_NAME="RemoteMaster"
BUILD_DIR="../../build"
DEPLOY_DIR="../../deploy/linux/package"
APP_DIR="$DEPLOY_DIR/usr/share/$APP_NAME"
EXEC_DIR="$DEPLOY_DIR/usr/bin"
DESKTOP_DIR="$DEPLOY_DIR/usr/share/applications"
ICON_DIR="$DEPLOY_DIR/usr/share/icons/hicolor"
QT_DIR="/usr/lib/x86_64-linux-gnu/qt6"

echo "Creating deployment package for Linux..."

# 创建部署目录
if [ -d "$DEPLOY_DIR" ]; then
    echo "Removing existing deployment directory..."
    rm -rf "$DEPLOY_DIR"
fi

echo "Creating deployment directory structure..."
mkdir -p "$DEPLOY_DIR"
mkdir -p "$APP_DIR"
mkdir -p "$EXEC_DIR"
mkdir -p "$DESKTOP_DIR"
mkdir -p "$ICON_DIR"

# 复制可执行文件
echo "Copying executable..."
cp "$BUILD_DIR/$APP_NAME" "$APP_DIR/"

# 创建符号链接到bin目录
ln -s "/usr/share/$APP_NAME/$APP_NAME" "$EXEC_DIR/$APP_NAME"

# 复制依赖库 (使用linuxdeployqt)
echo "Copying dependencies..."
if command -v linuxdeployqt &> /dev/null; then
    linuxdeployqt "$APP_DIR/$APP_NAME" -appimage
    echo "AppImage created successfully."
else
    echo "Warning: linuxdeployqt not found. Trying to copy Qt libraries manually..."
    # 尝试手动复制Qt库
    cp $QT_DIR/lib/libQt6Core.so* "$APP_DIR/" 2>/dev/null || true
    cp $QT_DIR/lib/libQt6Gui.so* "$APP_DIR/" 2>/dev/null || true
    cp $QT_DIR/lib/libQt6Network.so* "$APP_DIR/" 2>/dev/null || true
    cp $QT_DIR/lib/libQt6OpenGL.so* "$APP_DIR/" 2>/dev/null || true
    cp $QT_DIR/lib/libQt6Widgets.so* "$APP_DIR/" 2>/dev/null || true
fi

# 创建桌面文件
echo "Creating desktop file..."
cat > "$DESKTOP_DIR/$APP_NAME.desktop" << EOF
[Desktop Entry]
Name=RemoteMaster
Comment=Remote Desktop Application
Exec=/usr/bin/RemoteMaster
Icon=RemoteMaster
Terminal=false
Type=Application
Categories=Network;RemoteAccess;
EOF

# 复制图标文件
echo "Copying icon files..."
mkdir -p "$ICON_DIR/256x256/apps"
cp ../../resources/icons/application.png "$ICON_DIR/256x256/apps/RemoteMaster.png" 2>/dev/null || true

# 复制其他必要文件
echo "Copying additional files..."
cp ../../README.md "$APP_DIR/"
cp ../../LICENSE "$APP_DIR/" 2>/dev/null || true

# 创建deb包
echo "Creating DEB package..."
mkdir -p "$DEPLOY_DIR/DEBIAN"
cat > "$DEPLOY_DIR/DEBIAN/control" << EOF
Package: remotemaster
Version: 1.0.0
Section: net
Priority: optional
Architecture: amd64
Maintainer: RemoteMaster Team <info@remotemaster.example.com>
Description: Remote Desktop Application
 RemoteMaster is a powerful remote desktop application that supports
 VNC and RDP protocols, with multi-monitor support and true fullscreen mode.
EOF

# 设置权限
chmod 755 "$DEPLOY_DIR/DEBIAN"
chmod 755 "$EXEC_DIR/$APP_NAME"

# 构建deb包
if command -v dpkg-deb &> /dev/null; then
    dpkg-deb --build "$DEPLOY_DIR" "../../deploy/linux/${APP_NAME}_1.0.0_amd64.deb"
    echo "DEB package created successfully."
else
    echo "Warning: dpkg-deb not found. DEB package not created."
fi

# 创建AppImage
echo "Creating AppImage..."
if command -v appimagetool &> /dev/null; then
    mkdir -p "$DEPLOY_DIR/AppDir"
    cp -r "$APP_DIR"/* "$DEPLOY_DIR/AppDir/"
    cp ../../resources/icons/application.png "$DEPLOY_DIR/AppDir/" 2>/dev/null || true
    
    cat > "$DEPLOY_DIR/AppDir/AppRun" << EOF
#!/bin/bash
HERE=\$(dirname \$(readlink -f "\${0}"))
export LD_LIBRARY_PATH="\$HERE/lib:\$LD_LIBRARY_PATH"
exec "\$HERE/$APP_NAME" "\$@"
EOF
    
    chmod +x "$DEPLOY_DIR/AppDir/AppRun"
    
    appimagetool "$DEPLOY_DIR/AppDir" "../../deploy/linux/${APP_NAME}-x86_64.AppImage"
    echo "AppImage created successfully."
else
    echo "Warning: appimagetool not found. AppImage not created."
fi

echo "Linux deployment completed."